package top.hongthorn.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.DigestUtils;
import top.hongthorn.pojo.AircraftCarrier;
import top.hongthorn.pojo.User;
import top.hongthorn.mapper.UserMapper;
import top.hongthorn.service.UserService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import top.hongthorn.util.Result;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;

import java.util.HashMap;
import java.util.Map;

/**
 * <p>
 * 用户 服务实现类
 * </p>
 *
 * @author 泓宇网络科技工作室
 * @since 2023-12-23 21:11:10
 */
@Service
@Transactional
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {

    @Autowired
    private UserMapper userMapper;
    @Resource
    private HttpSession session;

    @Override
    public Result login(String username, String password) {
        User login = userMapper.login(username, DigestUtils.md5DigestAsHex(password.getBytes()));
        if (login != null && login.getState() == 2) {
            return Result.error(500, "账号已封禁");
        } else if (login != null) {
            session.setAttribute("jobType", login.getJobType());
            session.setAttribute("username", login.getUsername());
            login.setPassword("密码不展示");
            return Result.success(200, "登录成功", login);
        }
        return Result.error(500, "账号和密码错误");
    }

    @Override
    public Result register(String username, String password) {
        if (userMapper.findByUsername(username) == 1) {
            return Result.error(500, "用户名已存在，请更换一个", 3);
        }
        if (userMapper.register(username, DigestUtils.md5DigestAsHex(password.getBytes())) != 0) {
            return Result.success(200, "注册成功", 1);
        }
        return Result.error(500, "注册失败", 0);
    }

    @Override
    public Result updateUserPassword(String username, String password, String newPassword) {
        if (userMapper.login(username, DigestUtils.md5DigestAsHex(password.getBytes())) == null) {
            return Result.error(500, "原密码错误");
        } else {
            if (userMapper.updateUserPassword(username, DigestUtils.md5DigestAsHex(newPassword.getBytes())) != 0) {
                return Result.success(200, "修改成功", 1);
            }
        }
        return Result.error(500, "修改失败", 0);
    }

    @Override
    public Result addUser(User user) {
        user.setPassword(DigestUtils.md5DigestAsHex(user.getPassword().getBytes()));
        if (userMapper.findByUsername(user.getUsername()) == 1) {
            return Result.error(500, "用户名已存在，请更换一个", 3);
        }
        if (userMapper.insert(user) != 0) {
            return Result.success(200, "添加用户成功");
        }
        return Result.error(500, "添加用户失败");
    }

    @Override
    public Map userPage(Integer startIndex, Integer pageIndex) {
        Map<String, Object> map = new HashMap<>();
        Page<User> userPage = new Page<>(startIndex, pageIndex);
        IPage<User> iPage = this.page(userPage, null);
        map.put("code", 0);
        map.put("msg", "true");
        map.put("count", iPage.getTotal());
        map.put("data", iPage.getRecords());
        return map;
    }

    @Override
    public Result userState(Integer state, Integer id) {
        if (userMapper.userState(state, id) != 0) {
            return Result.success("状态修改成功");
        }
        return Result.error("状态修改失败");
    }

    @Override
    public Result deleteUser(Integer id) {
        if (userMapper.deleteUser(id) != 0) {
            return Result.success("删除成功");
        }
        return Result.error("删除失败");
    }

    @Override
    public Result updateUserInfo(User user) {
        if (userMapper.updateById(user) != 0) {
            return Result.success("修改成功");
        }
        return Result.error("修改失败");
    }
}